package com.mdsi.student;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mdsi.models.Absence;
import com.mdsi.models.ConnectionManager;
import com.mdsi.models.Department;
import com.mdsi.models.Lesson;
import com.mdsi.models.Room;
import com.mdsi.models.Student;
import com.mdsi.models.Subject;
import com.mdsi.models.Teacher;

/**
 * Servlet implementation class DetailAbsence
 */
@WebServlet("/User/Student/DetailAbsence")
public class DetailAbsence extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DetailAbsence() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("type") == null) {
			response.sendRedirect(request.getContextPath() + "/User/Student") ;
			return ;
		}
		Student student = (Student) request.getSession().getAttribute("user") ;
		student.cleanAbsence() ;
		try {
			Connection conn = ConnectionManager.getConnection() ;
			PreparedStatement st = conn.prepareStatement("SELECT A.ID_ABSENCE, A.JUSTIFY, A.JUSTIFICATION, A.LEVELJUSTIFICATION, "
						+ "A.LESSON.ID_LESSON, A.LESSON.START_HOUR, A.LESSON.END_HOUR, A.LESSON.OBLIGATORY, "
						+ "A.LESSON.SUBJECT.ID_SUBJECT, A.LESSON.SUBJECT.NAME, "
						+ "A.LESSON.ROOM.ID_ROOM, A.LESSON.ROOM.NUM, A.LESSON.ROOM.TYPE_ROOM, A.LESSON.ROOM.DEPT.ID_DEPARTMENT, A.LESSON.ROOM.DEPT.NAME, "
						+ "A.LESSON.TEACHER.ID_TEACHER, A.LESSON.TEACHER.FIRSTNAME, A.LESSON.TEACHER.LASTNAME, A.LESSON.TEACHER.EMAIL, "
						+ "A.LESSON.TEACHER.STATUT, A.LESSON.TEACHER.FUNCT, A.LESSON.TEACHER.IMMAT "
						+ "FROM ABSENCE A "
						+ "WHERE A.STUDENT.ID_STUDENT = " + student.getId()) ;
			ResultSet result = st.executeQuery() ;
			while (result.next()) {
				Calendar startHour = GregorianCalendar.getInstance() ;
				startHour.setTime(result.getDate("LESSON.START_HOUR")) ;
				Calendar endHour = GregorianCalendar.getInstance() ;
				endHour.setTime(result.getDate("LESSON.END_HOUR")) ;
				new Absence(result.getLong("ID_ABSENCE"), 
						new Lesson(result.getLong("LESSON.ID_LESSON"),
								new Subject (result.getLong("LESSON.SUBJECT.ID_SUBJECT"),
										result.getString("LESSON.SUBJECT.NAME")),
								new Room(result.getLong("LESSON.ROOM.ID_ROOM"), 
										Room.TypeRoom.valueOf(result.getString("LESSON.ROOM.TYPE_ROOM")), 
												new Department(result.getLong("LESSON.ROOM.DEPT.ID_DEPARTMENT"),
														result.getString("LESSON.ROOM.DEPT.NAME")), 
												result.getInt("LESSON.ROOM.NUM")),
								startHour,
								endHour,
								new Teacher(result.getLong("LESSON.TEACHER.ID_TEACHER"),
										result.getString("LESSON.TEACHER.FIRSTNAME"),
										result.getString("LESSON.TEACHER.LASTNAME"),
										result.getString("LESSON.TEACHER.EMAIL"),
										result.getLong("LESSON.TEACHER.IMMAT"),
										Teacher.StatutType.valueOf(result.getString("LESSON.TEACHER.STATUT")),
										result.getString("LESSON.TEACHER.FUNCT"))), 
						student, 
						"Y".equals(result.getString("JUSTIFY")), 
						result.getString("JUSTIFICATION"),
						result.getInt("LEVELJUSTIFICATION")) ;
			}
			conn.close () ;
			this.getServletContext().setAttribute("type", request.getParameter("type")) ;
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		this.getServletContext().getRequestDispatcher("/WEB-INF/student/detail_absence.jsp").forward(request, response);	
	}

}
